package com.banmao.desktoptools.baseScene.controller;

import com.banmao.desktoptools.baseView.Message;
import com.banmao.desktoptools.copyfilesScene.view.CopyFilesView;
import com.banmao.desktoptools.screenshotsPDFScene.controller.ScreenshotsPDFController;
import com.banmao.desktoptools.screenshotsPDFScene.view.ScreenshotsPDFView;
import com.banmao.desktoptools.util.AnnotationUtil;
import com.banmao.desktoptools.util.FXMLUtil;
import de.felixroske.jfxsupport.FXMLController;
import de.felixroske.jfxsupport.FXMLView;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Node;
import javafx.scene.control.Button;
import javafx.scene.control.Tab;
import javafx.scene.control.TabPane;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;

@Slf4j
@FXMLController
public class BaseSceneController implements Initializable {

    // tabPane
    public TabPane super_tabPane;

    public Button driving_test;

    @FXML
    private Tab tab_copyfiles;

    @FXML
    private Button show_copyfiles;

    @FXML
    private Button show_screenshotsPDF;

    @Autowired
    private CopyFilesView copyFilesView;

    @FXML
    void showCopyfilesView(ActionEvent event) {
        try {
            if(super_tabPane.getTabs().size() > 3) {
                Message message = new Message("提示", "tab栏已到达上限");
                message.show();
                log.info("生成tab已达到上限");
                return;
            }

            // 打开文件复制栏
            //FXMLLoader fxmlLoader = new FXMLLoader(BaseSceneController.class.getResource("/viewFxml/copyFilesView.fxml"));
            //Node load = fxmlLoader.load();

            String value = AnnotationUtil.getValue(CopyFilesView.class, FXMLView.class);
            if(StringUtils.isBlank(value)) {
                return;
            }

            Node node = FXMLUtil.loadFxml(value);
            if(node == null) {
                return;
            }

            StringBuilder tabName = new StringBuilder("文件复制");

            int count = 1;
            for (Tab tab : super_tabPane.getTabs()) {
                if(tab != null && tab.getText().contains(tabName)) {
                    count++;
                }
            }

            if (count != 1) {
                tabName.append(" ").append(count);
            }
            super_tabPane.getTabs().add(new Tab(tabName.toString(), node));

        } catch (IOException | InterruptedException e) {
            throw new RuntimeException(e);
        }

    }

    @FXML
    void showScreenshotsPDFView(ActionEvent event) {
        try {
            if(super_tabPane.getTabs().size() > 3) {
                Message message = new Message("提示", "tab栏已到达上限");
                message.show();
                log.info("生成tab已达到上限");
                return;
            }

            // 打开文件复制栏
            //FXMLLoader fxmlLoader = new FXMLLoader(BaseSceneController.class.getResource("/viewFxml/copyFilesView.fxml"));
            //Node load = fxmlLoader.load();

            String value = AnnotationUtil.getValue(ScreenshotsPDFView.class, FXMLView.class);
            if(StringUtils.isBlank(value)) {
                return;
            }

            Node node = FXMLUtil.loadFxml(value);
            if(node == null) {
                return;
            }

            StringBuilder tabName = new StringBuilder(show_screenshotsPDF.getText());

            int count = 1;
            for (Tab tab : super_tabPane.getTabs()) {
                if(tab != null && tab.getText().contains(tabName)) {
                    count++;
                }
            }

            if (count != 1) {
                tabName.append(" ").append(count);
            }
            super_tabPane.getTabs().add(new Tab(tabName.toString(), node));

        } catch (IOException | InterruptedException e) {
            throw new RuntimeException(e);
        }

    }

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        // 初始化时设置内容
        try {

            FXMLLoader fxmlLoader = new FXMLLoader();
            String value = AnnotationUtil.getValue(CopyFilesView.class, FXMLView.class);
            if(StringUtils.isBlank(value)) {
                throw new IllegalArgumentException("未找到资源");
            }

            Node load = FXMLUtil.loadFxml(value);

            tab_copyfiles.setContent(load);

        } catch (IOException e) {
            throw new RuntimeException(e);
        }

    }

}
